﻿using Coldairarrow.Business.A;
using Coldairarrow.Entity.A;
using Coldairarrow.Entity.PB;
using Coldairarrow.Entity.Print;
using Coldairarrow.Util;
using Coldairarrow.Util.ClassLibrary;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Options;
using System.Collections.Generic;
using System.Threading.Tasks;

namespace Coldairarrow.Api.Controllers.A
{
    [Route("/A/[controller]/[action]")]
    public class A_DemoController : BaseApiController
    {
        #region DI

        private readonly RedisOptions _redisOptions; //1.声明实体
        private readonly MongoDBOptions _mongodbOptions;

        public A_DemoController(IA_DemoBusiness a_DemoBus, IOptions<RedisOptions> redisOptions, IOptions<MongoDBOptions> mongodbOptions)//2.添加管道
        {
            _a_DemoBus = a_DemoBus;
            _redisOptions = redisOptions.Value;//3.赋值
            _mongodbOptions = mongodbOptions.Value;
        }

        IA_DemoBusiness _a_DemoBus { get; }

        #endregion

        #region 获取

        [HttpPost]
        public async Task<PageResult<A_Demo>> GetDataList(PageInput<ConditionDTO> input)
        {
            return await _a_DemoBus.GetDataListAsync(input); 
        }

        [HttpPost]
        public async Task<A_Demo> GetTheData(IdInputDTO input)
        {
            return await _a_DemoBus.GetTheDataAsync(input.id);
        }

        [HttpPost]
        public async Task FillLLData()
        {
            await _a_DemoBus.FillLLDataAsync();
        }

        #endregion

        #region 提交

        [HttpPost]
        public void SaveData(List<A_Demo> data)
        {
            List<A_Demo> newData = new List<A_Demo>();
            foreach (var item in data)
            {
                if (item.Id.IsNullOrEmpty())
                {
                    InitEntity(item);

                    //await _a_DemoBus.AddDataAsync(data);
                }
            }
            _a_DemoBus.AddDataAsync(data,_redisOptions);
        }
        /// <summary>
        /// 有条件更新
        /// </summary>
        /// <param name="data"></param>
        [HttpPost]
        public async Task SaveConditionDataAsync(A_Demo data)
        {
            //List<string> props = new List<string>();
            //foreach (System.Reflection.PropertyInfo p in data.GetType().GetProperties())
            //{
            //    if (!p.GetValue(data, null).IsNullOrEmpty())
            //    {
            //        if (!p.Name.Equals("Id"))
            //        {
            //            props.Add(p.Name);
            //        }
            //    }
            //}
            //if (data.Id.IsNullOrEmpty())
            //{
            //    InitEntity(data);

            //    await _a_DemoBus.UpdateDataAsync(data);
            //}
            await _a_DemoBus.UpdateDataAsync(data);
        }

        [HttpPost]
        public async Task DeleteData(List<string> ids)
        {
            await _a_DemoBus.DeleteDataAsync(ids);
        }

        [HttpPost]
        public async Task TestSqlData()
        {
            await _a_DemoBus.TestSqlDataAsync();
        }

        /// <summary>
        /// 测试新增MongoDB数据
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public async Task TestAddMongoDBData(PrintQueue data)
        {
            await _a_DemoBus.TestAddMongoDBDataAsync(data,_mongodbOptions);
        }

        #endregion
    }
}